package com.neuedu.myweather;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Hadoop 自定义数据类型
 *
 * @author
 */
public class WeatherWritable implements WritableComparable<WeatherWritable> {
    private int year;
    private double temperature;

    public WeatherWritable() {
    }

    public WeatherWritable(int year, double temperature) {
        this.year = year;
        this.temperature = temperature;
    }

    @Override
    public String toString() {
        return year + "\t" + temperature;
    }

    @Override
    public int compareTo(WeatherWritable other) {
        if (null == other) {
            return 1;
        }
        if (this.year != other.year) {
            return Integer.compare(this.year, other.year);
        }
        return Double.compare(this.temperature, other.temperature);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(this.year);
        out.writeDouble(this.temperature);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readInt();
        this.temperature = in.readDouble();
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public double getTemperature() {
        return temperature;
    }

    public void setTemperature(double temperature) {
        this.temperature = temperature;
    }
}
